package com.ruoyi.project.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Test;

import java.io.IOException;

public class TestHbase {

    @Test
    public void testCreate() throws IOException {
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "xxx");
        Connection connection = ConnectionFactory.createConnection(configuration);

        Admin admin = connection.getAdmin();

        HTableDescriptor test1 = new HTableDescriptor(TableName.valueOf("xx:xxx"));
        HColumnDescriptor firstColumnDescriptor = new HColumnDescriptor("xxx".getBytes());
        HColumnDescriptor secondColumnDescriptor = new HColumnDescriptor("xxx".getBytes());
        HColumnDescriptor thirdColumnDescriptor = new HColumnDescriptor("xxx".getBytes());
        test1.addFamily(firstColumnDescriptor);
        test1.addFamily(secondColumnDescriptor);
        test1.addFamily(thirdColumnDescriptor);

        admin.createTable(test1);
        admin.close();
        connection.close();
    }

    @Test
    public void testCreateNameSpace() throws IOException {
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "xxx");
        Connection connection = ConnectionFactory.createConnection(configuration);

        Admin admin = connection.getAdmin();

        NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create("xxx").build();
        admin.createNamespace(namespaceDescriptor);

        admin.close();
        connection.close();
    }

    @Test
    public void testPutData() throws IOException {
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "xxxx");
        Connection connection = ConnectionFactory.createConnection(configuration);


        //获取表对象
        Table table = connection.getTable(TableName.valueOf("xx:xxx"));

        Put put = new Put(Bytes.toBytes("xxx" + System.currentTimeMillis()));

        put.addColumn(Bytes.toBytes("xxx"), Bytes.toBytes("xx"), Bytes.toBytes("xx"));

        table.put(put);

        table.close();
        connection.close();


    }
}
